package com.example.allshengmedical.experimentanalyze.activity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.allsheng.dataprocess.AutoBaseline;
import com.example.allshengmedical.R;
import com.example.allshengmedical.R.color;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.experimentanalyze.bean.AdvancedSettingBean;
import com.example.allshengmedical.experimentanalyze.bean.CtSettingBean;
import com.example.allshengmedical.experimentnew.bean.LayoutTableBean;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.FileManagerUtil;
import com.example.allshengmedical.util.LogUtil;
import com.example.allshengmedical.util.MathLog;
import com.example.allshengmedical.util.SharedPreferencesUtils;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;

import android.R.integer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Environment;
import android.support.annotation.RequiresPermission.Write;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;


//分析中解析的工具
public class AmplificationUtil {

	// 数据
	String folderName;
	String fileName;
	private String appFilePath = Environment.getExternalStorageDirectory() + "/" + "AllShengMedical";
	StringBuffer contextString = new StringBuffer();

	public ArrayList<LayoutTableBean> activityLayoutTableBeans;

	private String defaultThreshold = "0.2";
	private String defaultStart = "3";
	private String defaultEnd = "15";
	public Boolean defaultThresholdBoo = true;
	public Boolean defaultBaselineBoo = true;

	public ArrayList<CtSettingBean> ctSettingBeans = new ArrayList<CtSettingBean>();
	public ArrayList<AdvancedSettingBean> advancedSettingBeans = new ArrayList<AdvancedSettingBean>();

	public List<HashMap<String, Object>> dataSourceListBlock1 = new ArrayList<HashMap<String, Object>>();
	public List<HashMap<String, Object>> dataSourceListBlock2 = new ArrayList<HashMap<String, Object>>();
	// 重复次数
	private int repititions;
	private ArrayList<ArrayList<LineDataSet>> dataListArrayList = new ArrayList<ArrayList<LineDataSet>>();

	//
	Activity activity;
	private AutoBaseline autoBaseline = new AutoBaseline();
	private File file;

	// activity页面的代码太多了。准备在这里生成Delta数据
	public ArrayList<ArrayList<LineDataSet>> dataListDeltaArrayList = new ArrayList<ArrayList<LineDataSet>>();
	private ArrayList<ArrayList<LineDataSet>> dataListDeltaLogArrayList = new ArrayList<ArrayList<LineDataSet>>();
	private ArrayList<ArrayList<LineDataSet>> dataListDeltaSampleArrayList = new ArrayList<ArrayList<LineDataSet>>();
	private ArrayList<ArrayList<LineDataSet>> dataListDeltaLogSampleArrayList = new ArrayList<ArrayList<LineDataSet>>();

	// ct数据
	public HashMap<Integer, LineDataSet> dataListCTArrayList = new HashMap<Integer, LineDataSet>();
	// private ArrayList<ArrayList<LineDataSet>> dataListCTLogArrayList = new
	// ArrayList<ArrayList<LineDataSet>>();
	// private HashMap<Integer, LineDataSet> dataListCTSampleArrayList = new
	// HashMap<Integer, LineDataSet>();
	// private ArrayList<ArrayList<LineDataSet>> dataListCTLogSampleArrayList =
	// new ArrayList<ArrayList<LineDataSet>>();

	LineDataSet dataSet = new LineDataSet(null, "");

	/*
	 * 判断文件是否包含参数设置的数据 如果没有创建 如果有就读取
	 */
	public void getAnalySetting(Activity activity, List<HashMap<String, Object>> dataSourceListBlock1,
			List<HashMap<String, Object>> dataSourceListBlock2, int repititions,
			ArrayList<ArrayList<LineDataSet>> dataListArrayList) {

		this.dataSourceListBlock1 = dataSourceListBlock1;
		this.dataSourceListBlock2 = dataSourceListBlock2;
		this.repititions = repititions;
		this.activity = activity;
		this.dataListArrayList = dataListArrayList;

		// 获取文件数据
		folderName = SharedPreferencesUtils.get(activity, BaseApplication.FILEMANGER_PREPAREFOLDERPATH, "") + "";
		fileName = SharedPreferencesUtils.get(activity, BaseApplication.FILEMANGER_PREPAREFILENAME, "") + "";

		String path = appFilePath + "/" + folderName;

		file = new File(path + "/" + fileName);
		FileInputStream in = null;
		BufferedReader reader = null;
		contextString = new StringBuffer();
		try {
			in = new FileInputStream(file);
			reader = new BufferedReader(new InputStreamReader(in));
			String line = "";
			while ((line = reader.readLine()) != null) {
				contextString.append(line);
				contextString.append(",");
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}

		// BufferedReader reader = null;
		// contextString=new StringBuffer();
		// System.gc();
		// try {
		// System.out.println("以行为单位读取文件内容，一次读一整行:");
		// reader = new BufferedReader(new FileReader(file));
		// String tempString = null;
		// int line = 1;
		// // 一次读入一行，直到读入null为文件结束
		// while ((tempString = reader.readLine()) != null) {
		// // 显示行号
		// // System.out.println("line " + line + ": " + tempString);
		// // Log.e("--", tempString);
		// contextString.append(tempString);
		// contextString.append(",");
		// line++;
		// }
		// reader.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// } finally {
		// if (reader != null) {
		// try {
		// reader.close();
		// // LogUtil.e("!!", contextString + "");
		//
		// } catch (IOException e1) {
		// }
		// }
		// }

		// 第一次进入am界面 判断文件是否有参数设置的值
		if (contextString.toString().contains("ParameterSetting")) {

			// 读取
			// ByAlert.alert("读取参数");
			readFile();

		} else {
			// 没有生成auto的数据
			// ByAlert.alert("生成新的参数");
			createData();
			writeFile();

		}

		getCtValue();
		getCtSampleValue();// 暂时不做

	}

	public  void getCtSampleValue() {

		// for (int i = 0; i < 32; i++) {
		// if (dataListCTArrayList.get(i) != null) {
		// dataListCTArrayList.get(i)
		// }
		// }

		// for (int i = 0; i < 32; i++) {
		// dataListCTArrayList
		// }

	}

	public  void getCtValue() {
//		if (dataListCTArrayList == null | dataListCTArrayList.size() == 0) {

			generateDeltaline(dataListArrayList, repititions);

			// layoutOne的数据解析
			activityLayoutTableBeans = new ArrayList<LayoutTableBean>();

			// LogUtil.e(contextString.indexOf(",Aisle") + 7);
			// LogUtil.e(contextString.indexOf(",Color"));

			int index = 0;
			while (!(contextString.indexOf("Aisle", index) <= 0)) {

				// Aisle:
				String aisle;
				aisle = contextString.substring(contextString.indexOf(",Aisle", index) + 7,

						contextString.indexOf(",Color", index));
						// LogUtil.e(aisle);

				// Color
				String color;
				color = contextString.substring(contextString.indexOf(",Color", index) + 7,

						contextString.indexOf(",Target", index));
						// LogUtil.e(color);

				// Target
				String target;
				target = contextString.substring(contextString.indexOf(",Target", index) + 8,

						contextString.indexOf(",Dyes", index));

				// LogUtil.e(target);

				int nextIndex = contextString.indexOf(",Aisle", index) + 7;

				// Dyes
				String dyes;
				if (contextString.indexOf(",Aisle", nextIndex) < 0) {

					dyes = contextString.substring(contextString.indexOf(",Dyes", index) + 6,
							contextString.indexOf(",,dataSourceListBlock1", index));

					// LogUtil.e(dyes);
				} else {

					dyes = contextString.substring(contextString.indexOf(",Dyes", index) + 6,

							contextString.indexOf(",Aisle", nextIndex));
					// LogUtil.e(dyes);
				}

				index = contextString.indexOf(",Dyes", index) + 5;

				activityLayoutTableBeans.add(new LayoutTableBean(false, Integer.parseInt(aisle.trim()),
						Integer.parseInt(color.trim()), target, dyes));

			}

			// dataListCTArrayList
			// dataListCTSampleArrayList

			// CT数据只有line 没log 所以spinner三的无用了。
			// ct数据的x轴最多为32（根据基因盘的数量来的）

			// autoBaseline.getCtValue(pY, nTotal, m_threhold);
			// 分两个基因盘遍历

			double m_pY[] = new double[repititions];
			double ctValue = 0;
			for (int i = 0; i < 16; i++) {
				for (int j = 0; j < activityLayoutTableBeans.size(); j++) {

					m_pY = new double[repititions];
					ctValue = 0;

					if (TextUtils.isEmpty(dataSourceListBlock1.get(i).get("item_image") + "")) {
						ArrayList<Entry> entries = new ArrayList<Entry>();
						Entry yVals = new Entry(0, i);
						entries.add(yVals);

						LineDataSet lineDataSet = new LineDataSet(entries, "");
						lineDataSet.setCircleSize(5);
						lineDataSet.setCircleColorHole(Color.RED);
						lineDataSet.setValueTextSize(0);
						lineDataSet.setCircleColor(Color.RED);
						lineDataSet.setValueTextColor(Color.WHITE);

						try {
							dataListCTArrayList.get(i).getYValForXIndex(i);

							if (dataListCTArrayList.get(i).getYValForXIndex(i) != 0) {

							} else {

								dataListCTArrayList.put(i, lineDataSet);
							}

						} catch (Exception e) {
							dataListCTArrayList.put(i, lineDataSet);
						}
					} else {
						// AB基因盘
						if (dataSourceListBlock1.get(i).get("item_image")
								.equals(activityLayoutTableBeans.get(j).getColor() + "")) {
							if (activityLayoutTableBeans.get(j).getAisle() == 1) {

								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i).get(0).getYValForXIndex(x);
								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
										
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}

							} else if (activityLayoutTableBeans.get(j).getAisle() == 2) {
								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i).get(1).getYValForXIndex(x);

								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}
							} else if (activityLayoutTableBeans.get(j).getAisle() == 3) {
								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i).get(2).getYValForXIndex(x);

								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}
							} else if (activityLayoutTableBeans.get(j).getAisle() == 4) {
								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i).get(3).getYValForXIndex(x);

								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}
							}
							// 要将数据带进曲线了
							// dataListCTArrayList只有一个通道
							ArrayList<Entry> entries = new ArrayList<Entry>();
							Entry yVals = new Entry(Float.parseFloat(ctValue + ""), i);
							entries.add(yVals);

							LineDataSet lineDataSet = new LineDataSet(entries, "");
							lineDataSet.setCircleSize(5);
							lineDataSet.setCircleColorHole(Color.RED);
							lineDataSet.setValueTextSize(0);
							lineDataSet.setCircleColor(Color.RED);
							lineDataSet.setValueTextColor(Color.WHITE);

							dataListCTArrayList.put(i, lineDataSet);
						} else {
							ArrayList<Entry> entries = new ArrayList<Entry>();
							Entry yVals = new Entry(0, i);
							entries.add(yVals);

							LineDataSet lineDataSet = new LineDataSet(entries, "");
							lineDataSet.setCircleSize(5);
							lineDataSet.setCircleColorHole(Color.RED);
							lineDataSet.setValueTextSize(0);
							lineDataSet.setCircleColor(Color.RED);
							lineDataSet.setValueTextColor(Color.WHITE);

							try {
								dataListCTArrayList.get(i).getYValForXIndex(i);
								if (dataListCTArrayList.get(i).getYValForXIndex(i) != 0) {

								} else {
									dataListCTArrayList.put(i, lineDataSet);
								}
							} catch (Exception e) {

							}

						}
					}

				}

				// // cd基因盘
				// if (dataSourceListBlock2.get(i).get("item_image")
				// .equals(activityLayoutTableBeans.get(j).getColor())) {
				//
				// }

			}
			for (int i = 0; i < 16; i++) {
				for (int j = 0; j < activityLayoutTableBeans.size(); j++) {

					m_pY = new double[repititions];
					ctValue = 0;
					// AB基因盘
					if (TextUtils.isEmpty(dataSourceListBlock2.get(i).get("item_image") + "")) {
						ArrayList<Entry> entries = new ArrayList<Entry>();
						Entry yVals = new Entry(0, i + 16);
						entries.add(yVals);

						LineDataSet lineDataSet = new LineDataSet(entries, "");
						lineDataSet.setCircleSize(5);
						lineDataSet.setCircleColorHole(Color.RED);
						lineDataSet.setValueTextSize(0);
						lineDataSet.setCircleColor(Color.RED);
						lineDataSet.setValueTextColor(Color.WHITE);

						try {
							dataListCTArrayList.get(i + 16).getYValForXIndex(i + 16);

							if (dataListCTArrayList.get(i + 16).getYValForXIndex(i + 16) != 0) {

							} else {

								dataListCTArrayList.put(i + 16, lineDataSet);
							}

						} catch (Exception e) {
							dataListCTArrayList.put(i + 16, lineDataSet);
						}
					} else {

						if (dataSourceListBlock2.get(i).get("item_image")
								.equals(activityLayoutTableBeans.get(j).getColor() + "")) {
							if (activityLayoutTableBeans.get(j).getAisle() == 1) {

								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i + 16).get(0).getYValForXIndex(x);
								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}

							} else if (activityLayoutTableBeans.get(j).getAisle() == 2) {
								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i + 16).get(1).getYValForXIndex(x);

								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}
							} else if (activityLayoutTableBeans.get(j).getAisle() == 3) {
								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i + 16).get(2).getYValForXIndex(x);

								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}
							} else if (activityLayoutTableBeans.get(j).getAisle() == 4) {
								for (int x = 0; x < repititions; x++) {
									m_pY[x] = dataListDeltaArrayList.get(i + 16).get(3).getYValForXIndex(x);

								}
								for (int x = 0; x < ctSettingBeans.size(); x++) {
									if (activityLayoutTableBeans.get(j).getTarget()
											.equals(ctSettingBeans.get(x).getTargetName())) {

										if (ctSettingBeans.get(x).isAutoThreshold()) {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													autoBaseline.getThreholdValue(m_pY, repititions,
															autoBaseline.getBaselineStart(m_pY, repititions),
															autoBaseline.getBaselineEnd(m_pY, repititions)));
											break;
										} else {
											ctValue = autoBaseline.getCtValue(m_pY, repititions,
													Double.parseDouble(ctSettingBeans.get(x).getThreshold()+""));
											break;
										}

									}
								}
							}
							// 要将数据带进曲线了
							// dataListCTArrayList只有一个通道
							ArrayList<Entry> entries = new ArrayList<Entry>();
							Entry yVals = new Entry(Float.parseFloat(ctValue + ""), i + 16);
							entries.add(yVals);

							LineDataSet lineDataSet = new LineDataSet(entries, "");
							lineDataSet.setCircleSize(5);
							lineDataSet.setCircleColorHole(Color.RED);
							lineDataSet.setCircleColor(Color.RED);
							lineDataSet.setValueTextSize(0);
							lineDataSet.setValueTextColor(Color.WHITE);

							dataListCTArrayList.put(i + 16, lineDataSet);
						} else {
							ArrayList<Entry> entries = new ArrayList<Entry>();
							Entry yVals = new Entry(0, i + 16);
							entries.add(yVals);

							LineDataSet lineDataSet = new LineDataSet(entries, "");
							lineDataSet.setCircleSize(5);
							lineDataSet.setCircleColorHole(Color.RED);
							lineDataSet.setValueTextSize(0);
							lineDataSet.setCircleColor(Color.RED);
							lineDataSet.setValueTextColor(Color.WHITE);

							try {
								dataListCTArrayList.get(i + 16).getYValForXIndex(i + 16);

								if (dataListCTArrayList.get(i + 16).getYValForXIndex(i + 16) != 0) {

								} else {

									dataListCTArrayList.put(i + 16, lineDataSet);
									// dataListCTArrayList.size();
								}

							} catch (Exception e) {

							}
						}

					}

				}

				// // cd基因盘
				// if (dataSourceListBlock2.get(i).get("item_image")
				// .equals(activityLayoutTableBeans.get(j).getColor())) {
				//
				// }

			}
//		}

	}

	private void readFile() {

		// 获取

		if (contextString.toString().contains("ParameterSetting,,default,")) {

			contextString = new StringBuffer(contextString
					.substring(contextString.indexOf("ParameterSetting,,default,") + 26, contextString.length()));

			// default
			defaultThresholdBoo = Boolean.parseBoolean(contextString.substring(
					contextString.indexOf("defaultThresholdBoo:") + 20, contextString.indexOf(",defaultThreshold:")));
			defaultThreshold = contextString.subSequence(contextString.indexOf("defaultThreshold:") + 17,
					contextString.indexOf(",defaultBaselineBoo:")).toString().trim();
			defaultBaselineBoo = Boolean.parseBoolean(contextString.substring(
					contextString.indexOf("defaultBaselineBoo:") + 19, contextString.indexOf(",defaultStart:")));
			defaultStart = contextString
					.substring(contextString.indexOf("defaultStart:") + 13, contextString.indexOf(",defaultEnd:"))
					.toString().trim();
			defaultEnd = contextString.substring(contextString.indexOf("defaultEnd:") + 11,
					contextString.indexOf(",,ctSettingBeans"));

			// ctSettingBeans
			contextString = new StringBuffer(contextString.substring(contextString.indexOf(",,ctSettingBeans,") + 17,
					contextString.indexOf(",,advancedSettingBeans")));
			int index = 0;
			while (contextString.indexOf("targetName:", index) > 0) {

				String targetName = contextString.substring(contextString.indexOf("targetName:", index) + 11,
						contextString.indexOf(",threshold:", index));
				String threshold = contextString.substring(contextString.indexOf("threshold:", index) + 10,
						contextString.indexOf(",baselineStart:", index));
				String baselineStart = contextString.substring(contextString.indexOf("baselineStart:", index) + 14,
						contextString.indexOf(",baselineEnd:", index));
				String baselineEnd = contextString.substring(contextString.indexOf("baselineEnd:", index) + 12,
						contextString.indexOf(",useDaultSetting:", index));
				Boolean useDaultSetting = Boolean
						.parseBoolean(contextString.substring(contextString.indexOf("useDaultSetting:", index) + 16,
								contextString.indexOf(",isAutoThreshold:", index)));
				Boolean isAutoThreshold = Boolean
						.parseBoolean(contextString.substring(contextString.indexOf("isAutoThreshold:", index) + 16,
								contextString.indexOf(",isAutoBaseLine:", index)));

				int nextIndex = contextString.indexOf("isAutoBaseLine:", index) + 17;
				if (contextString.indexOf(",targetName:", nextIndex) < 0) {
					Boolean isAutoBaseLine = Boolean.parseBoolean(contextString
							.substring(contextString.indexOf("isAutoBaseLine:", index) + 15, contextString.length()));
					// for (CtSettingBean bean : ctSettingBeans) {
					// if (bean.getTargetName().equals(targetName)) {
					// bean.setThreshold(threshold);
					// bean.setBaselineStart(baselineStart);
					// bean.setBaselineEnd(baselineEnd);
					// bean.setUseDaultSetting(useDaultSetting);
					// bean.setAutoThreshold(isAutoThreshold);
					// bean.setAutoBaseLine(isAutoBaseLine);
					// }
					// }
					ctSettingBeans.add(new CtSettingBean(targetName, threshold, baselineStart, baselineEnd,
							useDaultSetting, isAutoThreshold, isAutoBaseLine));

				} else {
					Boolean isAutoBaseLine = Boolean
							.parseBoolean(contextString.substring(contextString.indexOf("isAutoBaseLine:", index) + 15,
									contextString.indexOf(",targetName:", nextIndex)));
					// for (CtSettingBean bean : ctSettingBeans) {
					// if (bean.getTargetName().equals(targetName)) {
					// bean.setThreshold(threshold);
					// bean.setBaselineStart(baselineStart);
					// bean.setBaselineEnd(baselineEnd);
					// bean.setUseDaultSetting(useDaultSetting);
					// bean.setAutoThreshold(isAutoThreshold);
					// bean.setAutoBaseLine(isAutoBaseLine);
					// }
					// }
					ctSettingBeans.add(new CtSettingBean(targetName, threshold, baselineStart, baselineEnd,
							useDaultSetting, isAutoThreshold, isAutoBaseLine));

				}

				index = contextString.indexOf("isAutoBaseLine:", index) + 19;

			}

		}
		contextString = new StringBuffer();
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行:");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				// System.out.println("line " + line + ": " + tempString);
				// Log.e("--", tempString);
				contextString.append(tempString);
				contextString.append(",");
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
					// LogUtil.e("!!", contextString + "");

				} catch (IOException e1) {
				}
			}
		}

		// 获取

		if (contextString.toString().contains("advancedSettingBeans")) {

			String advanceContextString = contextString.substring(contextString.indexOf(",,advancedSettingBeans,") + 23,
					contextString.length());

			// advancedSettingBeans
			int index = 0;
			while (advanceContextString.indexOf("well:", index) > 0) {

				// well:A1
				// targetName:target
				// baseLineName:AUTO
				// baselineStart:AUTO
				// baselineEnd:AUTO
				// useCTSetingBoo:true
				// autoBaselineBoolean:true
				String well = advanceContextString.substring(advanceContextString.indexOf("well:", index) + 5,
						advanceContextString.indexOf(",targetName:", index));
				String targetName = advanceContextString.substring(
						advanceContextString.indexOf("targetName:", index) + 11,
						advanceContextString.indexOf(",baseLineName:", index));
				String baseLineName = advanceContextString.substring(
						advanceContextString.indexOf("baseLineName:", index) + 13,
						advanceContextString.indexOf(",baselineStart:", index));
				String baselineStart = advanceContextString.substring(
						advanceContextString.indexOf("baselineStart:", index) + 14,
						advanceContextString.indexOf(",baselineEnd:", index));
				String baselineEnd = advanceContextString.substring(
						advanceContextString.indexOf("baselineEnd:", index) + 12,
						advanceContextString.indexOf(",useCTSetingBoo:", index));
				Boolean useCTSetingBoo = Boolean.parseBoolean(
						advanceContextString.substring(advanceContextString.indexOf("useCTSetingBoo:", index) + 15,
								advanceContextString.indexOf(",autoBaselineBoolean:", index)));

				int nextIndex = advanceContextString.indexOf("autoBaselineBoolean:", index) + 20;
				if (advanceContextString.indexOf(",well:", nextIndex) < 0) {
					Boolean autoBaselineBoolean = Boolean.parseBoolean(advanceContextString.substring(
							advanceContextString.indexOf("autoBaselineBoolean:", index) + 20,
							advanceContextString.length() - 1));

					// for (AdvancedSettingBean bean : advancedSettingBeans) {
					// if (bean.getWell().equals(well)) {
					// bean.setTargetName(targetName);
					// bean.setBaseLine(baseLineName);
					// bean.setBaselineStart(baselineStart);
					// bean.setBaselineEndString(baselineEnd);
					// bean.setUseCTSetingBoo(useCTSetingBoo);
					// bean.setAutoBaselineBoolean(autoBaselineBoolean);
					//
					// }
					// }
					advancedSettingBeans.add(new AdvancedSettingBean(well, targetName, baseLineName, baselineStart,
							baselineEnd, useCTSetingBoo, autoBaselineBoolean));

				} else {
					Boolean autoBaselineBoolean = Boolean.parseBoolean(advanceContextString.substring(
							advanceContextString.indexOf("autoBaselineBoolean:", index) + 20,
							advanceContextString.indexOf(",well:", nextIndex)));

					// for (AdvancedSettingBean bean : advancedSettingBeans) {
					// if (bean.getWell().equals(well)) {
					// bean.setTargetName(targetName);
					// bean.setBaseLine(baseLineName);
					// bean.setBaselineStart(baselineStart);
					// bean.setBaselineEndString(baselineEnd);
					// bean.setUseCTSetingBoo(useCTSetingBoo);
					// bean.setAutoBaselineBoolean(autoBaselineBoolean);
					//
					// }
					// }
					advancedSettingBeans.add(new AdvancedSettingBean(well, targetName, baseLineName, baselineStart,
							baselineEnd, useCTSetingBoo, autoBaselineBoolean));

				}

				index = advanceContextString.indexOf("autoBaselineBoolean:", index) + 21;

			}

		}
	}

	private void createData() {

		// layoutOne的数据解析
		activityLayoutTableBeans = new ArrayList<LayoutTableBean>();

		// LogUtil.e(contextString.indexOf(",Aisle") + 7);
		// LogUtil.e(contextString.indexOf(",Color"));

		int index = 0;
		while (!(contextString.indexOf("Aisle", index) <= 0)) {

			// Aisle:
			String aisle;
			aisle = contextString.substring(contextString.indexOf(",Aisle", index) + 7,

					contextString.indexOf(",Color", index));
					// LogUtil.e(aisle);

			// Color
			String color;
			color = contextString.substring(contextString.indexOf(",Color", index) + 7,

					contextString.indexOf(",Target", index));
					// LogUtil.e(color);

			// Target
			String target;
			target = contextString.substring(contextString.indexOf(",Target", index) + 8,

					contextString.indexOf(",Dyes", index));

			// LogUtil.e(target);

			int nextIndex = contextString.indexOf(",Aisle", index) + 7;

			// Dyes
			String dyes;
			if (contextString.indexOf(",Aisle", nextIndex) < 0) {

				dyes = contextString.substring(contextString.indexOf(",Dyes", index) + 6,
						contextString.indexOf(",,dataSourceListBlock1", index));

				// LogUtil.e(dyes);
			} else {

				dyes = contextString.substring(contextString.indexOf(",Dyes", index) + 6,

						contextString.indexOf(",Aisle", nextIndex));
				// LogUtil.e(dyes);
			}

			index = contextString.indexOf(",Dyes", index) + 5;

			activityLayoutTableBeans.add(new LayoutTableBean(false, Integer.parseInt(aisle.trim()),
					Integer.parseInt(color.trim()), target, dyes));

		}

		// for (int i = 0; i < 32; i++) {
		// // advancedSettingBeans.add
		// }

		// a ,b
		for (int i = 0; i < 16; i++) {

			for (LayoutTableBean bean : activityLayoutTableBeans) {

				if (!TextUtils.isEmpty(dataSourceListBlock1.get(i).get("item_image").toString())) {
					if (Integer.parseInt(dataSourceListBlock1.get(i).get("item_image").toString()) == bean.getColor()) {
						double[] pY = new double[repititions];
						int index3 = 0;
						if (dataListArrayList.get(i).get(bean.getAisle() - 1) != null) {
							for (Entry entry : dataListArrayList.get(i).get(bean.getAisle() - 1).getYVals()) {
								pY[index3] = entry.getVal();
								index3++;
							}
						}

						if (i < 8) {
							int index1 = i + 1;
							advancedSettingBeans.add(new AdvancedSettingBean("A" + index1, bean.getTarget(), "AUTO",
									autoBaseline.getBaselineStart(pY, repititions) + "",
									autoBaseline.getBaselineEnd(pY, repititions) + "", true, true));
						} else {
							int index2 = i - 7;
							advancedSettingBeans.add(new AdvancedSettingBean("B" + index2, bean.getTarget(), "AUTO",
									autoBaseline.getBaselineStart(pY, repititions) + "",
									autoBaseline.getBaselineEnd(pY, repititions) + "", true, true));
						}

					}
				} else {

					double[] pY = new double[repititions];
					int index3 = 0;
					if (dataListArrayList.get(i).get(bean.getAisle() - 1) != null) {
						for (Entry entry : dataListArrayList.get(i).get(bean.getAisle() - 1).getYVals()) {
							pY[index3] = entry.getVal();
							index3++;
						}
					}

					if (i < 8) {
						int index1 = i + 1;
						advancedSettingBeans.add(new AdvancedSettingBean("A" + index1, "", "", "", "", true, true));
					} else {
						int index2 = i - 7;
						advancedSettingBeans.add(new AdvancedSettingBean("B" + index2, "", "", "", "", true, true));
					}
					break;
				}

			}

		}

		// c ,d
		for (int i = 0; i < 16; i++) {
			for (LayoutTableBean bean : activityLayoutTableBeans) {

				if (!TextUtils.isEmpty(dataSourceListBlock2.get(i).get("item_image").toString())) {
					if (Integer.parseInt(dataSourceListBlock2.get(i).get("item_image").toString()) == bean.getColor()) {
						double[] pY = new double[repititions];
						int index3 = 0;
						if (dataListArrayList.get(i).get(bean.getAisle() - 1) != null) {
							for (Entry entry : dataListArrayList.get(i).get(bean.getAisle() - 1).getYVals()) {
								pY[index3] = entry.getVal();
								index3++;
							}
						}
						if (i < 8) {
							int index1 = i + 1;
							advancedSettingBeans.add(new AdvancedSettingBean("C" + index1, bean.getTarget(), "AUTO",
									autoBaseline.getBaselineStart(pY, repititions) + "",
									autoBaseline.getBaselineEnd(pY, repititions) + "", true, true));
						} else {
							int index2 = i - 7;
							advancedSettingBeans.add(new AdvancedSettingBean("D" + index2, bean.getTarget(), "AUTO",
									autoBaseline.getBaselineStart(pY, repititions) + "",
									autoBaseline.getBaselineEnd(pY, repititions) + "", true, true));
						}

					}
				} else {

					double[] pY = new double[repititions];
					int index3 = 0;
					if (dataListArrayList.get(i).get(bean.getAisle() - 1) != null) {
						for (Entry entry : dataListArrayList.get(i).get(bean.getAisle() - 1).getYVals()) {
							pY[index3] = entry.getVal();
							index3++;
						}
					}
					if (i < 8) {
						int index1 = i + 1;
						advancedSettingBeans.add(new AdvancedSettingBean("C" + index1, "", "", "", "", true, true));
					} else {
						int index2 = i - 7;
						advancedSettingBeans.add(new AdvancedSettingBean("D" + index2, "", "", "", "", true, true));
					}

					break;
				}
			}

		}

		for (int i = 0; i < activityLayoutTableBeans.size(); i++) {
			int ctNum = 0;
			double sumNum = 0;
			for (int j = 0; j < 16; j++) {
				// for (HashMap<String, Object> bean : dataSourceListBlock1) {
				if (dataSourceListBlock1.get(j).get("item_text").toString().equals("Neg.")
						|| dataSourceListBlock1.get(j).get("item_text").toString().equals("Pos.")) {

				} else {
					if (TextUtils.isEmpty(dataSourceListBlock1.get(j).get("item_image").toString().trim() + "")) {

					} else {
						if (activityLayoutTableBeans.get(i).getColor() == Integer
								.parseInt(dataSourceListBlock1.get(j).get("item_image").toString().trim() + "")) {
							double[] pY = new double[repititions];
							int index1 = 0;
							if (dataListArrayList.get(j).get(activityLayoutTableBeans.get(i).getAisle() - 1) != null) {
								for (Entry entry : dataListArrayList.get(j)
										.get(activityLayoutTableBeans.get(i).getAisle() - 1).getYVals()) {
									pY[index1] = entry.getVal();
									index1++;

								}
								System.out.println();
							}

							sumNum = sumNum + autoBaseline.getThreholdValue(pY, repititions,
									autoBaseline.getBaselineStart(pY, repititions),
									autoBaseline.getBaselineEnd(pY, repititions));
							ctNum++;
						}
					}
				}

			}
			for (int j = 0; j < 16; j++) {
				// for (HashMap<String, Object> bean : dataSourceListBlock2) {
				if (dataSourceListBlock2.get(j).get("item_text").toString().equals("Neg.")
						|| dataSourceListBlock2.get(j).get("item_text").toString().equals("Pos.")) {

				} else {
					if (TextUtils.isEmpty(dataSourceListBlock2.get(j).get("item_image").toString().trim() + "")) {

					} else {
						if (activityLayoutTableBeans.get(i).getColor() == Integer
								.parseInt(dataSourceListBlock2.get(j).get("item_image").toString().trim() + "")) {
							double[] pY = new double[repititions];
							int index1 = 0;
							if (dataListArrayList.get(j).get(activityLayoutTableBeans.get(i).getAisle() - 1) != null) {
								for (Entry entry : dataListArrayList.get(j)
										.get(activityLayoutTableBeans.get(i).getAisle() - 1).getYVals()) {
									pY[index1] = entry.getVal();
									index1++;

								}
								System.out.println();
							}

							// autoBaseline.getThreholdValue(pY, repititions,
							// autoBaseline.getBaselineStart(pY, repititions),
							// autoBaseline.getBaselineEnd(pY, repititions));
							sumNum = sumNum + autoBaseline.getThreholdValue(pY, repititions,
									autoBaseline.getBaselineStart(pY, repititions),
									autoBaseline.getBaselineEnd(pY, repititions));
							ctNum++;
						}
					}
				}

			}

			ctSettingBeans.add(new CtSettingBean(activityLayoutTableBeans.get(i).getTarget(), sumNum / ctNum + "", "1",
					repititions + ""));
		}
		ctSettingBeans.get(0).setSelected(true);

	}

	private void writeFile() {
		// 写文件 将数据写进文件

		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		// 创建文件
		String folderName = SharedPreferencesUtils.get(activity, BaseApplication.FILEMANGER_PREPAREFOLDERPATH, "0")
				+ "";
		String fileName = SharedPreferencesUtils.get(activity, BaseApplication.FILEMANGER_PREPAREFILENAME, "0") + "";

		// 这个方法和之前的一样，先一行一行的全部取出来。然后当遇到本页面数据。我们不做添加然后在这时我们要加入接下来的文件的数据
		String path = appFilePath + "/" + folderName;
		File file = new File(path + "/" + fileName);
		BufferedReader reader = null;
		StringBuffer writeText = new StringBuffer();
		contextString = new StringBuffer();
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行:");
			reader = new BufferedReader(new FileReader(file));

			String tempString = "";
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			Boolean layoutOneBoolean = false;
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				// System.out.println("line " + line + ": "
				// +
				// tempString);
				// Log.e("--", tempString);
				if (tempString.contains("ParameterSetting")) {
					break;
				}

				writeText.append(tempString);
				writeText.append("\r\n");
				line++;
			}

			reader.close();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
					// LogUtil.e("!!", contextString + "");

				} catch (IOException e1) {
				}
			}
		}

		// StringBuffer content = new StringBuffer();

		writeText.append("\r\n" + "ParameterSetting" + "\r\n");
		writeText.append("\r\n" + "default" + "\r\n");
		writeText.append("\r\n" + "defaultThresholdBoo" + ":" + defaultThresholdBoo);
		writeText.append("\r\n" + "defaultThreshold" + ":" + defaultThreshold);
		writeText.append("\r\n" + "defaultBaselineBoo" + ":" + defaultBaselineBoo);
		writeText.append("\r\n" + "defaultStart" + ":" + defaultStart);
		writeText.append("\r\n" + "defaultEnd" + ":" + defaultEnd);

		writeText.append("\r\n" + "\r\n" + "ctSettingBeans" + "\r\n");
		for (int i = 0; i < ctSettingBeans.size(); i++) {
			writeText.append("\r\n" + "targetName" + ":" + ctSettingBeans.get(i).getTargetName());
			writeText.append("\r\n" + "threshold" + ":" + ctSettingBeans.get(i).getThreshold());
			writeText.append("\r\n" + "baselineStart" + ":" + ctSettingBeans.get(i).getBaselineStart());
			writeText.append("\r\n" + "baselineEnd" + ":" + ctSettingBeans.get(i).getBaselineEnd());
			writeText.append("\r\n" + "useDaultSetting" + ":" + ctSettingBeans.get(i).isUseDaultSetting());
			writeText.append("\r\n" + "isAutoThreshold" + ":" + ctSettingBeans.get(i).isAutoThreshold());
			writeText.append("\r\n" + "isAutoBaseLine" + ":" + ctSettingBeans.get(i).isAutoBaseLine());

		}
		if (advancedSettingBeans != null && advancedSettingBeans.size() > 0) {
			writeText.append("\r\n" + "\r\n" + "advancedSettingBeans" + "\r\n");
			for (int i = 0; i < advancedSettingBeans.size(); i++) {
				writeText.append("\r\n" + "well" + ":" + advancedSettingBeans.get(i).getWell());
				writeText.append("\r\n" + "targetName" + ":" + advancedSettingBeans.get(i).getTargetName());
				writeText.append("\r\n" + "baseLineName" + ":" + advancedSettingBeans.get(i).getBaseLine());
				writeText.append("\r\n" + "baselineStart" + ":" + advancedSettingBeans.get(i).getBaselineStart());
				writeText.append("\r\n" + "baselineEnd" + ":" + advancedSettingBeans.get(i).getBaselineEndString());
				writeText.append("\r\n" + "useCTSetingBoo" + ":" + advancedSettingBeans.get(i).getUseCTSetingBoo());
				writeText.append(
						"\r\n" + "autoBaselineBoolean" + ":" + advancedSettingBeans.get(i).getAutoBaselineBoolean());
			}

		}

		fileManagerUtil.createFIle(folderName, fileName);
		fileManagerUtil.writeFile(folderName, fileName, "" + writeText, false);
	}

	public void getAutoBaseLine(Boolean boo, int position, ArrayList<LayoutTableBean> activityLayoutTableBeans,
			String targetName) {

//		if (ctSettingBeans == null || ctSettingBeans.size() == 0) {

			if (boo) {
				ctSettingBeans.get(position).setAutoBaseLine(boo);

				for (int i = 0; i < activityLayoutTableBeans.size(); i++) {
					if (activityLayoutTableBeans.get(i).getTarget().equals(targetName)) {
						double[] pY = new double[repititions];
						int index1 = 0;
						for (Entry entry : dataListArrayList.get(i).get(activityLayoutTableBeans.get(i).getAisle())
								.getYVals()) {
							pY[index1] = entry.getVal();
							index1++;

						}
						ctSettingBeans.get(position)
								.setBaselineStart(autoBaseline.getBaselineStart(pY, repititions) + "");
						ctSettingBeans.get(position).setBaselineEnd(autoBaseline.getBaselineEnd(pY, repititions) + "");
						// 改advance
						for (AdvancedSettingBean advancedSettingBean : advancedSettingBeans) {
							if (advancedSettingBean.getTargetName().equals(targetName)) {
								if (advancedSettingBean.getUseCTSetingBoo()) {
									advancedSettingBean
											.setBaselineStart(autoBaseline.getBaselineStart(pY, repititions) + "");
									advancedSettingBean
											.setBaselineEndString(autoBaseline.getBaselineEnd(pY, repititions) + "");
								} else {

								}
							}

						}

					}

				}

			} else {
				ctSettingBeans.get(position).setAutoBaseLine(boo);
				ctSettingBeans.get(position).setBaselineStart(1 + "");
				ctSettingBeans.get(position).setBaselineEnd(repititions + "");
				// 改advance
				for (AdvancedSettingBean advancedSettingBean : advancedSettingBeans) {
					if (advancedSettingBean.getTargetName().equals(targetName)) {
						if (advancedSettingBean.getUseCTSetingBoo()) {
							advancedSettingBean.setBaselineStart(1 + "");
							advancedSettingBean.setBaselineEndString(repititions + "");
						} else {

						}
					}

				}

			}
//		}
	}

	public float getAutoNumberTv(String targetName, ArrayList<LayoutTableBean> activityLayoutTableBeans) {

		this.activityLayoutTableBeans = activityLayoutTableBeans;

		// for (int i = 0; i < activityLayoutTableBeans.size(); i++) {
		// int ctNum = 0;
		// double sumNum = 0;
		// for (HashMap<String, Object> bean : dataSourceListBlock1) {
		// if (TextUtils.isEmpty(bean.get("item_image").toString().trim()
		// + "")) {
		//
		// } else {
		// if (activityLayoutTableBeans.get(i).getColor() == Integer
		// .parseInt(bean.get("item_image").toString().trim()
		// + "")) {
		// double[] pY = new double[repititions];
		// int index1 = 0;
		// if (dataListArrayList.get(i).get(
		// activityLayoutTableBeans.get(i).getAisle()-1) != null) {
		// for (Entry entry : dataListArrayList
		// .get(i)
		// .get(activityLayoutTableBeans.get(i)
		// .getAisle()-1).getYVals()) {
		// pY[index1] = entry.getVal();
		// index1++;
		//
		// }
		// }
		//
		// sumNum = sumNum
		// + autoBaseline.getThreholdValue(pY,
		// repititions, autoBaseline
		// .getBaselineStart(pY,
		// repititions),
		// autoBaseline.getBaselineEnd(pY,
		// repititions));
		// ctNum++;
		// }
		// }
		//
		// }
		// for (HashMap<String, Object> bean : dataSourceListBlock2) {
		// if (TextUtils.isEmpty(bean.get("item_image").toString().trim()
		// + "")) {
		//
		// } else {
		// if (activityLayoutTableBeans.get(i).getColor() == Integer
		// .parseInt(bean.get("item_image").toString().trim()
		// + "")) {
		// double[] pY = new double[repititions];
		// int index1 = 0;
		// if (dataListArrayList.get(i).get(
		// activityLayoutTableBeans.get(i).getAisle()-1) != null) {
		// for (Entry entry : dataListArrayList
		// .get(i)
		// .get(activityLayoutTableBeans.get(i)
		// .getAisle()-1).getYVals()) {
		// pY[index1] = entry.getVal();
		// index1++;
		//
		// }
		// }
		//
		// autoBaseline.getThreholdValue(pY, repititions,
		// autoBaseline.getBaselineStart(pY, repititions),
		// autoBaseline.getBaselineEnd(pY, repititions));
		// sumNum = sumNum
		// + autoBaseline.getThreholdValue(pY,
		// repititions, autoBaseline
		// .getBaselineStart(pY,
		// repititions),
		// autoBaseline.getBaselineEnd(pY,
		// repititions));
		// ctNum++;
		// }
		// }
		//
		// }
		// for (int j = 0; j < ctSettingBeans.size(); j++) {
		// if (ctSettingBeans.get(j).getTargetName().equals(targetName)) {
		// ctSettingBeans.set(j, new CtSettingBean(
		// activityLayoutTableBeans.get(j).getTarget(), sumNum
		// / ctNum + "", "1", "1"));
		// }
		// }
		// }
		ArrayList<CtSettingBean> ctSettingBeansCopy = new ArrayList<CtSettingBean>();

		for (int i = 0; i < activityLayoutTableBeans.size(); i++) {
			int ctNum = 0;
			double sumNum = 0;
//			ArrayList<Double> doubles =new ArrayList<Double>();
			for (int j = 0; j < 16; j++) {
				// for (HashMap<String, Object> bean : dataSourceListBlock1) {
				if (dataSourceListBlock1.get(j).get("item_text").toString().equals("Neg.")
						|| dataSourceListBlock1.get(j).get("item_text").toString().equals("Pos.")) {

				} else {
					if (TextUtils.isEmpty(dataSourceListBlock1.get(j).get("item_image").toString().trim() + "")) {

					} else {
						if (activityLayoutTableBeans.get(i).getColor() == Integer
								.parseInt(dataSourceListBlock1.get(j).get("item_image").toString().trim() + "")) {
							double[] pY = new double[repititions];
							int index1 = 0;
							if (dataListArrayList.get(j).get(activityLayoutTableBeans.get(i).getAisle() - 1) != null) {
								for (Entry entry : dataListArrayList.get(j)
										.get(activityLayoutTableBeans.get(i).getAisle() - 1).getYVals()) {
									pY[index1] = entry.getVal();
									index1++;

								}
							}
		
							sumNum = sumNum + autoBaseline.getThreholdValue(pY, repititions,
									autoBaseline.getBaselineStart(pY, repititions),
									autoBaseline.getBaselineEnd(pY, repititions));
//							doubles.add(autoBaseline.getThreholdValue(pY, repititions,
//									autoBaseline.getBaselineStart(pY, repititions),
//									autoBaseline.getBaselineEnd(pY, repititions)));
							ctNum++;
						}
					}
				}

			}
			for (int j = 0; j < 16; j++) {
				// for (HashMap<String, Object> bean : dataSourceListBlock2) {
				if (dataSourceListBlock2.get(j).get("item_text").toString().equals("Neg.")
						|| dataSourceListBlock2.get(j).get("item_text").toString().equals("Pos.")) {

				} else {
					if (TextUtils.isEmpty(dataSourceListBlock2.get(j).get("item_image").toString().trim() + "")) {

					} else {
						if (activityLayoutTableBeans.get(i).getColor() == Integer
								.parseInt(dataSourceListBlock2.get(j).get("item_image").toString().trim() + "")) {
							double[] pY = new double[repititions];
							int index1 = 0;
							if (dataListArrayList.get(j).get(activityLayoutTableBeans.get(i).getAisle() - 1) != null) {
								for (Entry entry : dataListArrayList.get(j)
										.get(activityLayoutTableBeans.get(i).getAisle() - 1).getYVals()) {
									pY[index1] = entry.getVal();
									index1++;

								}
							}

							// autoBaseline.getThreholdValue(pY, repititions,
							// autoBaseline.getBaselineStart(pY, repititions),
							// autoBaseline.getBaselineEnd(pY, repititions));
							sumNum = sumNum + autoBaseline.getThreholdValue(pY, repititions,
									autoBaseline.getBaselineStart(pY, repititions),
									autoBaseline.getBaselineEnd(pY, repititions));
//							doubles.add(autoBaseline.getThreholdValue(pY, repititions,
//									autoBaseline.getBaselineStart(pY, repititions),
//									autoBaseline.getBaselineEnd(pY, repititions)));
							ctNum++;
						}
					}
				}

			}

			ctSettingBeansCopy.add(new CtSettingBean(activityLayoutTableBeans.get(i).getTarget(), sumNum / ctNum + "",
					"1", repititions + ""));
		}
		float result = 0;
		for (int i = 0; i < ctSettingBeansCopy.size(); i++) {
			if (ctSettingBeansCopy.get(i).getTargetName().equals(targetName)) {
				result = Float.parseFloat(ctSettingBeansCopy.get(i).getThreshold());
			}
		}
		// for(int i= 0;i<ctSettingBeans.size();i++){
		// if(ctSettingBeans.get(i).getTargetName().equals(targetName)){
		// result=Float.parseFloat(ctSettingBeans.get(i).getThreshold());
		// }
		// }
		return result;

	}

	public void generateDeltaline(ArrayList<ArrayList<LineDataSet>> dataListArrayList, int repititions) {

		this.dataListArrayList = dataListArrayList;
		this.repititions = repititions;
		double m_pY[] = new double[repititions];
		ArrayList<double[]> pys = new ArrayList<double[]>();

		// dataListDeltaArrayList
		// dataListDeltaLogArrayList
		// dataListDeltaSampleArrayList
		// dataListDeltaLogSampleArrayList

		// dataListDeltaArrayList
		// 现在我要取出所有的y轴值
		// 先分32个基因盘 再分四个通道
		for (int i = 0; i < 32; i++) {
			// 每个基因盘的4条线数据的集合。
			ArrayList<LineDataSet> linshiLineDataSets = new ArrayList<LineDataSet>();
			pys = new ArrayList<double[]>();
			// 取出一条线4个通道
			for (int j = 0; j < 4; j++) {
				m_pY = new double[repititions];
				// 取出一条线一个通道
				for (int x = 0; x < repititions; x++) {
					if (dataListArrayList.get(i).get(j) != null) {
						m_pY[x] = dataListArrayList.get(i).get(j).getYValForXIndex(x);
					}

				}
				pys.add(autoBaseline.getDeltaValue(m_pY, repititions));
				// 把数据存入dataListDeltaArrayList

			}
			// 获取颜色

			String color = "";
			if (i <= 15) {
				color = dataSourceListBlock1.get(i).get("item_image").toString();
			} else {
				color = dataSourceListBlock2.get(i - 16).get("item_image").toString();
			}

			for (int j = 0; j < pys.size(); j++) {
				// LineDataSet lineDataSet =new LineDataSet();
				ArrayList<Entry> yValuesDelta = new ArrayList<Entry>();
				// 一条线里所有的点
				for (int x = 0; x < pys.get(j).length; x++) {
					Entry entry = new Entry((float) pys.get(j)[x], x);
					yValuesDelta.add(entry);
				}
				addLineChart(yValuesDelta, color, 1);
				linshiLineDataSets.add(dataSet);

			}

			dataListDeltaArrayList.add(linshiLineDataSets);
		}

		// dataListDeltaLogArrayList
		// 现在我要取出所有的y轴值
		// 先分32个基因盘 再分四个通道
		// 上面的临时数据要清空
		m_pY = new double[repititions];
		pys = new ArrayList<double[]>();
		for (int i = 0; i < 32; i++) {
			// 每个基因盘的4条线数据的集合。
			ArrayList<LineDataSet> linshiLineDataSets = new ArrayList<LineDataSet>();
			pys = new ArrayList<double[]>();
			// 取出一条线4个通道
			for (int j = 0; j < 4; j++) {
				m_pY = new double[repititions];
				// 取出一条线一个通道
				for (int x = 0; x < repititions; x++) {
					m_pY[x] = dataListDeltaArrayList.get(i).get(j).getYValForXIndex(x);

				}
				pys.add(autoBaseline.getLogValue(m_pY, repititions));
				// 把数据存入dataListDeltaArrayList

			}
			// 获取颜色

			String color = "";
			if (i <= 15) {
				color = dataSourceListBlock1.get(i).get("item_image").toString();
			} else {
				color = dataSourceListBlock2.get(i - 16).get("item_image").toString();
			}

			for (int j = 0; j < pys.size(); j++) {
				// LineDataSet lineDataSet =new LineDataSet();
				ArrayList<Entry> yValuesDelta = new ArrayList<Entry>();
				// 一条线里所有的点
				for (int x = 0; x < pys.get(j).length; x++) {
					Entry entry = new Entry((float) pys.get(j)[x], x);
					yValuesDelta.add(entry);
				}
				addLineChart(yValuesDelta, color, 1);
				linshiLineDataSets.add(dataSet);

			}

			dataListDeltaLogArrayList.add(linshiLineDataSets);
		}

		// dataListDeltaSampleArrayList
		// sample数据分两次做 16 一次 16 一次

		for (int x = 0; x < 16; x++) {

			ArrayList<LineDataSet> dataList = null;
			dataList = new ArrayList<LineDataSet>();
			String color = "";
			if ("sample".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#ff2d55";
			} else if ("standard".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#fd9727";
			} else if ("positive".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#ff00a9";
			} else if ("negative".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#159588";

			} else {
				color = "#ffffff";
			}

			for (int j = 0; j < 4; j++) {
				ArrayList<Entry> yValueslog = new ArrayList<Entry>();

				for (int i = 0; i < repititions; i++) {
					// bean.get(0).getYValForXIndex(i);
					// bean.get(j).getYValForXIndex(i);
					// yValueslog.clear();
					// bean.get(j).getYVals().get(i).getVal();
					yValueslog.add(new Entry(dataListDeltaArrayList.get(x).get(j).getYValForXIndex(i), i));

				}
				addLineChart(yValueslog, color, 2);
				// dataLista1.add(dataSet);
				dataList.add(dataSet);

			}
			dataListDeltaSampleArrayList.add(dataList);

		}
		for (int x = 0; x < 16; x++) {

			ArrayList<LineDataSet> dataList = null;
			dataList = new ArrayList<LineDataSet>();
			String color = "";
			if ("sample".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#ff2d55";
			} else if ("standard".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#fd9727";
			} else if ("positive".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#ff00a9";
			} else if ("negative".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#159588";

			} else {
				color = "#ffffff";
			}

			for (int j = 0; j < 4; j++) {
				ArrayList<Entry> yValueslog = new ArrayList<Entry>();

				for (int i = 0; i < repititions; i++) {
					// bean.get(0).getYValForXIndex(i);
					// bean.get(j).getYValForXIndex(i);
					// yValueslog.clear();
					// bean.get(j).getYVals().get(i).getVal();
					yValueslog.add(new Entry(dataListDeltaArrayList.get(x + 16).get(j).getYValForXIndex(i), i));

				}
				addLineChart(yValueslog, color, 2);
				// dataLista1.add(dataSet);
				dataList.add(dataSet);

			}
			dataListDeltaSampleArrayList.add(dataList);

		}

		// dataListDeltaLogSampleArrayList
		// sample数据分两次做 16 一次 16 一次

		for (int x = 0; x < 16; x++) {

			ArrayList<LineDataSet> dataList = null;
			dataList = new ArrayList<LineDataSet>();
			String color = "";
			if ("sample".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#ff2d55";
			} else if ("standard".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#fd9727";
			} else if ("positive".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#ff00a9";
			} else if ("negative".equals(dataSourceListBlock1.get(x).get("item_type"))) {
				color = "#159588";

			} else {
				color = "#ffffff";
			}

			for (int j = 0; j < 4; j++) {
				ArrayList<Entry> yValueslog = new ArrayList<Entry>();

				for (int i = 0; i < repititions; i++) {
					// bean.get(0).getYValForXIndex(i);
					// bean.get(j).getYValForXIndex(i);
					// yValueslog.clear();
					// bean.get(j).getYVals().get(i).getVal();
					yValueslog.add(new Entry(dataListDeltaLogArrayList.get(x).get(j).getYValForXIndex(i), i));

				}
				addLineChart(yValueslog, color, 2);
				// dataLista1.add(dataSet);
				dataList.add(dataSet);

			}
			dataListDeltaLogSampleArrayList.add(dataList);

		}
		for (int x = 0; x < 16; x++) {

			ArrayList<LineDataSet> dataList = null;
			dataList = new ArrayList<LineDataSet>();
			String color = "";
			if ("sample".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#ff2d55";
			} else if ("standard".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#fd9727";
			} else if ("positive".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#ff00a9";
			} else if ("negative".equals(dataSourceListBlock2.get(x).get("item_type"))) {
				color = "#159588";

			} else {
				color = "#ffffff";
			}

			for (int j = 0; j < 4; j++) {
				ArrayList<Entry> yValueslog = new ArrayList<Entry>();

				for (int i = 0; i < repititions; i++) {
					// bean.get(0).getYValForXIndex(i);
					// bean.get(j).getYValForXIndex(i);
					// yValueslog.clear();
					// bean.get(j).getYVals().get(i).getVal();
					yValueslog.add(new Entry(dataListDeltaLogArrayList.get(x + 16).get(j).getYValForXIndex(i), i));

				}
				addLineChart(yValueslog, color, 2);
				// dataLista1.add(dataSet);
				dataList.add(dataSet);

			}
			dataListDeltaLogSampleArrayList.add(dataList);

		}

	}

	private void addLineChart(ArrayList<Entry> yValues, String colorString, int type) {
		dataSet = new LineDataSet(yValues, "");
		// dataSet.setLineWidth(0);
		if (type == 1) {
			if (TextUtils.isEmpty(colorString)) {
				dataSet.setColor(color.white);
			} else {
				dataSet.setColor(BaseApplication.getContext().getResources().getColor(getcolor(colorString)));
			}
		} else if (type == 2) {
			if (TextUtils.isEmpty(colorString)) {
				dataSet.setColor(color.white);
			} else {
				dataSet.setColor(Color.parseColor(colorString));
			}
		}

		dataSet.setCircleSize(0);
		dataSet.setValueTextSize(0);
		dataSet.setLineWidth(1);
	}

	private int getcolor(String colorString) {
		switch (Integer.parseInt(colorString)) {
		case 11:
			return R.color.palette_one_one;

		case 12:

			return R.color.palette_one_two;
		case 13:

			return R.color.palette_one_three;
		case 14:

			return R.color.palette_one_four;
		case 15:

			return R.color.palette_one_five;
		case 16:

			return R.color.palette_one_six;
		case 21:

			return R.color.palette_two_one;
		case 22:

			return R.color.palette_two_two;
		case 23:

			return R.color.palette_two_three;
		case 24:

			return R.color.palette_two_four;
		case 25:

			return R.color.palette_two_five;
		case 26:

			return R.color.palette_two_six;
		case 31:
			return R.color.palette_three_one;
		case 32:

			return R.color.palette_three_two;
		case 33:

			return R.color.palette_three_three;
		case 34:

			return R.color.palette_three_four;
		case 35:

			return R.color.palette_three_five;
		case 36:

			return R.color.palette_three_six;
		case 41:
			return R.color.palette_four_one;

		case 42:

			return R.color.palette_four_two;
		case 43:

			return R.color.palette_four_three;
		case 44:

			return R.color.palette_four_four;
		case 45:

			return R.color.palette_four_five;
		case 46:

			return R.color.palette_four_six;
		case 51:

			return R.color.palette_five_one;
		case 52:

			return R.color.palette_five_two;
		case 53:

			return R.color.palette_five_three;
		case 54:

			return R.color.palette_five_four;
		case 55:

			return R.color.palette_five_five;
		case 56:

			return R.color.palette_five_six;
		case 61:

			return R.color.palette_six_one;
		case 62:

			return R.color.palette_six_two;
		case 63:

			return R.color.palette_six_three;
		case 64:

			return R.color.palette_six_four;
		case 65:

			return R.color.palette_six_five;
		case 66:

			return R.color.palette_six_six;

		default:
			return 0;
		}

	}

	public String getFolderName() {
		return folderName;
	}

	public void setFolderName(String folderName) {
		this.folderName = folderName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getAppFilePath() {
		return appFilePath;
	}

	public void setAppFilePath(String appFilePath) {
		this.appFilePath = appFilePath;
	}

	public StringBuffer getContextString() {
		return contextString;
	}

	public void setContextString(StringBuffer contextString) {
		this.contextString = contextString;
	}

	public ArrayList<LayoutTableBean> getActivityLayoutTableBeans() {
		return activityLayoutTableBeans;
	}

	public void setActivityLayoutTableBeans(ArrayList<LayoutTableBean> activityLayoutTableBeans) {
		this.activityLayoutTableBeans = activityLayoutTableBeans;
	}

	public String getDefaultThreshold() {
		return defaultThreshold;
	}

	public void setDefaultThreshold(String defaultThreshold) {
		this.defaultThreshold = defaultThreshold;
	}

	public String getDefaultStart() {
		return defaultStart;
	}

	public void setDefaultStart(String defaultStart) {
		this.defaultStart = defaultStart;
	}

	public String getDefaultEnd() {
		return defaultEnd;
	}

	public void setDefaultEnd(String defaultEnd) {
		this.defaultEnd = defaultEnd;
	}

	public Boolean getDefaultThresholdBoo() {
		return defaultThresholdBoo;
	}

	public void setDefaultThresholdBoo(Boolean defaultThresholdBoo) {
		this.defaultThresholdBoo = defaultThresholdBoo;
	}

	public Boolean getDefaultBaselineBoo() {
		return defaultBaselineBoo;
	}

	public void setDefaultBaselineBoo(Boolean defaultBaselineBoo) {
		this.defaultBaselineBoo = defaultBaselineBoo;
	}

	public ArrayList<CtSettingBean> getCtSettingBeans() {
		return ctSettingBeans;
	}

	public void setCtSettingBeans(ArrayList<CtSettingBean> ctSettingBeans) {
		this.ctSettingBeans = ctSettingBeans;
	}

	public ArrayList<AdvancedSettingBean> getAdvancedSettingBeans() {
		return advancedSettingBeans;
	}

	public void setAdvancedSettingBeans(ArrayList<AdvancedSettingBean> advancedSettingBeans) {
		this.advancedSettingBeans = advancedSettingBeans;
	}

	public List<HashMap<String, Object>> getDataSourceListBlock1() {
		return dataSourceListBlock1;
	}

	public void setDataSourceListBlock1(List<HashMap<String, Object>> dataSourceListBlock1) {
		this.dataSourceListBlock1 = dataSourceListBlock1;
	}

	public List<HashMap<String, Object>> getDataSourceListBlock2() {
		return dataSourceListBlock2;
	}

	public void setDataSourceListBlock2(List<HashMap<String, Object>> dataSourceListBlock2) {
		this.dataSourceListBlock2 = dataSourceListBlock2;
	}

	public int getRepititions() {
		return repititions;
	}

	public void setRepititions(int repititions) {
		this.repititions = repititions;
	}

	public ArrayList<ArrayList<LineDataSet>> getDataListArrayList() {
		return dataListArrayList;
	}

	public void setDataListArrayList(ArrayList<ArrayList<LineDataSet>> dataListArrayList) {
		this.dataListArrayList = dataListArrayList;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public ArrayList<ArrayList<LineDataSet>> getDataListDeltaArrayList() {
		return dataListDeltaArrayList;
	}

	public void setDataListDeltaArrayList(ArrayList<ArrayList<LineDataSet>> dataListDeltaArrayList) {
		this.dataListDeltaArrayList = dataListDeltaArrayList;
	}

	public ArrayList<ArrayList<LineDataSet>> getDataListDeltaLogArrayList() {
		return dataListDeltaLogArrayList;
	}

	public void setDataListDeltaLogArrayList(ArrayList<ArrayList<LineDataSet>> dataListDeltaLogArrayList) {
		this.dataListDeltaLogArrayList = dataListDeltaLogArrayList;
	}

	public ArrayList<ArrayList<LineDataSet>> getDataListDeltaSampleArrayList() {
		return dataListDeltaSampleArrayList;
	}

	public void setDataListDeltaSampleArrayList(ArrayList<ArrayList<LineDataSet>> dataListDeltaSampleArrayList) {
		this.dataListDeltaSampleArrayList = dataListDeltaSampleArrayList;
	}

	public ArrayList<ArrayList<LineDataSet>> getDataListDeltaLogSampleArrayList() {
		return dataListDeltaLogSampleArrayList;
	}

	public void setDataListDeltaLogSampleArrayList(ArrayList<ArrayList<LineDataSet>> dataListDeltaLogSampleArrayList) {
		this.dataListDeltaLogSampleArrayList = dataListDeltaLogSampleArrayList;
	}

	public HashMap<Integer, LineDataSet> getDataListCTArrayList() {
		return dataListCTArrayList;
	}

	public void setDataListCTArrayList(HashMap<Integer, LineDataSet> dataListCTArrayList) {
		this.dataListCTArrayList = dataListCTArrayList;
	}

}
